cd ${work}5_reg_analysis

*** CONSTRUCT TABLE 2
*(Table 2 is constructed manually based on the output in output\table_2.log)

use input\sample_ipt, clear
destring jurnr, gen(jurnr_num)

*MERGE ON OUTCOMES FROM DE LOECKER ET AL. PROCEDURE
merge 1:1 jurnr_num vnr unit year using input\vars_ipt, keep(3) keepusing(sales log_quantity log_mu log_mc rho exit_x) nogen

*EXCLUDE SMALL FLOWS
keep if sales>7.5

*EXCLUDE OUTLIERS OF ESTIMATED MARKUPS (FOLLOWING DE LOECKER ET AL.)
keep if exit_x>0 | exit_x==.
drop exit_x
winsor2 log_mu, cuts(3 97) trim replace
keep if log_mu!=.

*EXPORT SUPPORT
merge m:1 jurnr_num vnr year using input\promotion_ipt, keep(1 3) keepusing(treated_1 tdcinitiative_1) nogen
merge m:1 jurnr_num year using input\promotion_it, keep(1 3) keepusing(itemvalue_1 discount_1) nogen
foreach v of varlist treated_1 tdcinitiative_1 itemvalue_1 discount_1 {
    replace `v'=0 if `v'==.
}
drop jurnr_num

gen 	group = 1 if treated_1==0 & tdcinitiative_1==0
replace	group = 2 if treated_1==0 & tdcinitiative_1==1
replace	group = 3 if treated_1==1 & tdcinitiative_1==0
replace	group = 4 if treated_1==1 & tdcinitiative_1==1

*OUTCOMES
gen log_sales = log(sales)
gen log_price = log(sales /1e3) - log_quantity									
gen log_quality = exp(log_mu)*log_price + (exp(log_mu)-1)*log_quantity

*CREATE PRODUCT-LEVEL SUPPORT EXPENDITURES
bysort jurnr year : egen temp = total(treated_1)
gen itemvalue_ipt = (itemvalue_1 - discount_1) * treated_1/temp
replace itemvalue_ipt = 0 if itemvalue_ipt==.
	foreach v of varlist itemvalue_1 discount_1 {
		replace `v'=`v' * treated_1/temp
		replace `v'=0 if `v'==.
	}
drop treated_1 temp itemvalue_1 discount_1

*PROFIT MEASURES
gen var_profits = (1-1/exp(log_mu)) * sales
gen var_profits_nos = var_profits - itemvalue_ipt
drop var_profits

*scale down expenditures and wage bill by the ratio between total firm revenue and total product sales
merge m:1 jurnr year using input\fire_it, assert(2 3) keep(3) keepusing(revenue wagebill expenditures) nogen
bysort jurnr year : egen product_sales = total(sales)
gen product_share = product_sales / revenue
replace product_share = 1 if product_share>1
gen profits = sales - rho*product_share*(wagebill+expenditures) - itemvalue_ipt
drop itemvalue_ipt sales revenue wagebill expenditures product_sales product_share rho

*IHS
foreach v of varlist var_profits_nos profits {
	gen double `v'_temp = `v'
    replace `v'_temp=`v'_temp/1e3
	deflate `v'_temp
	gen ihs_`v' = log(`v'_temp+sqrt(`v'_temp^2+1))
	drop `v'_temp
}

*PURGE
egen PU=group(vnr unit)	
foreach v of varlist log_quantity log_price log_mc log_quality {
	
	qui reghdfe `v', a(PU) res(res)
	replace `v'=res
	drop res
	
}

*WINSORIZE
winsor2 log_quantity log_price log_mc log_quality log_mu ihs_var_profits ihs_profits , cuts(1 99) replace

*RE-CENTER WINSORIZED VARIABLES
foreach v of varlist log_quantity log_price log_mc log_quality {
    qui sum `v'
	replace `v'=`v'-r(mean)
}

*TABLE
qui {
log using output\table_2 , text replace 
noi tabstat log_quantity log_price log_mc log_mu log_quality ihs_var_profits_nos ihs_profits , by(group) s(mean sd) f(%9.3f)
noi tab group
log close
}